﻿<?php 
	require_once('wxbrower.php');
	require_once("../includes/public.inc.php");
	//require_once("../includes/conn.php");
	$s=isset($_GET['s'])?$_GET['s']:'';
	if (!isset($_GET['code']))
	{
		$uwxcode=$_GET['uwxcode'];
		$locaurl=urlencode(WEBNAME."main/zhengpin.php?s=".$s."&uwxcode=".$uwxcode);//网址解析
		$urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx934cb7f852f6c585&redirect_uri=".$locaurl."&response_type=code&scope=snsapi_base#wechat_redirect";
		header("Location:".$urls);exit;
	}
	$code = $_GET['code'];//获取code
	$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx934cb7f852f6c585&secret=94ea78cbbbd72c8a0ecbfc965a497afc&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
	$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
	$array = get_object_vars($jsondecode);//转换成数组
	$openid = $array['openid'];//输出openid
	//echo $openid;exit;
	//开始是查询防伪码的
	
function getAccessToken()
{
 $data = json_decode(file_get_contents("access_token.json"));
    if ($data->expire_time < time()) {
    	
      $url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx934cb7f852f6c585&secret=94ea78cbbbd72c8a0ecbfc965a497afc';
	  $json=json_decode(curlGet($url_get),true);
	if (!isset($json['errmsg']))
	{
			$access_token = $json['access_token'];
	      if ($access_token) {
	        $data->expire_time = time() + 7000;
	        $data->access_token = $access_token;
	        $fp = fopen("access_token.json", "w");
	        fwrite($fp, json_encode($data));
	        fclose($fp);
	      }
	}
	else 
	{
		echo '获取access_token发生错误：错误代码,微信返回错误信息：';
	}
    } else {
      $access_token = $data->access_token;
    }
	
	return $access_token;
}

function curlGet($url)
{
	$curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}

$access_token=getAccessToken();	//echo $access_token;exit;
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid;
$subscribe = json_decode(curlGet($subscribe_msg),true);
$zyxx = $subscribe['subscribe'];
if($zyxx !== 1)
{
	header("Location: ../user/guanzhu.php");exit;
}
//判断是否关注
if($_SESSION['username']&&$_SESSION['password'])
{
	$exsit_openid = mysql_fetch_array(mysql_query("select * from ".DBQIAN."daogou where username='".trim($_SESSION['username'])."' and password='".trim($_SESSION['password'])."' order by id desc limit 1"));
	$exsit_uopenid = mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where username='".trim($_SESSION['username'])."' and password='".trim($_SESSION['password'])."' order by id desc limit 1"));
}
else 
{
	$exsit_openid = mysql_fetch_array(mysql_query("select * from ".DBQIAN."daogou where wx_openid='".$openid."' order by id desc limit 1"));
	$exsit_uopenid = mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where openid='".$openid."' order by id desc limit 1"));
}
	if(!$exsit_openid && !$exsit_uopenid)//如果没有注册或者没有openid，跳到选择登录的页面
{
	header("Location: ../user/zhengpin.php?s=".$s."&openid=".$openid);exit;//如果两者都没有那么则可以直接跳登录页面
}
else 
{
	if ($exsit_openid)//导购
	{
		if ($s)
		{
			$exsits_fwm = mysql_fetch_array(mysql_query("select * from ".DBQIAN."fwm where fwm=".$s." and dgstatus=0 order by id desc limit 1"));
			if ($exsits_fwm)
			{
				$sccode = $exsits_fwm['fwm'];
			}
			else 
			{
				$exsits_fwm2 = mysql_fetch_array(mysql_query("select * from ".DBQIAN."fwm where fwm=".$s." and dgstatus=1 order by id desc limit 1"));
				if ($exsits_fwm2)
				{
					header("Location: jiamao.php?status=2");exit;
				}
				else
				{
					header("Location: jiamao.php?status=3");exit;
				}
			}
			//$urow=$dbconn->fetch($dbconn->query("select uheadimgurl from ".DBQIAN."user_list where ucode='$ucode' order by id desc limit 1"));
		}
		else 
		{
			header("Location: jiamao.php?status=1");exit;
		}
		if ($exsit_openid['status'] == 0)//没审核
		{
			header("Location: ../user/check.php");exit;
		}
		else 
		{
			$_SESSION['username'] = $exsit_uopenid['username'];
			$_SESSION['password'] = $exsit_uopenid['password'];
			$_SESSION['openid'] = $openid;
			//可以直接跳转到领红包页面。暂时不适用，都是要跳转，直接在本页面领取积分
		}
	}
	else //一般会员，肯定是存在信息并且登陆成功才能领取
	{
		if ($s)
		{
			//一般会员是不需要使用防伪码进行领取红包的，只需要有足够的积分/但是这是个入口文件，领取积分和红包是统一的，都需要判断真伪
			$exsits_fwm = mysql_fetch_array(mysql_query("select * from ".DBQIAN."fwm where fwm=".$s." and hystatus=0 order by id desc limit 1"));
			if ($exsits_fwm)
			{
				$sccode = $exsits_fwm['fwm'];
			}
			else 
			{
				$exsits_fwm2 = mysql_fetch_array(mysql_query("select * from ".DBQIAN."fwm where fwm=".$s." and hystatus=1 order by id desc limit 1"));
				if ($exsits_fwm2)
				{
					header("Location: jiamao.php?status=2");exit;
				}
				else
				{
					header("Location: jiamao.php?status=3");exit;
				}
			}
			//$urow=$dbconn->fetch($dbconn->query("select uheadimgurl from ".DBQIAN."user_list where ucode='$ucode' order by id desc limit 1"));
		}
		else 
		{
			header("Location: jiamao.php?status=1");exit;
		}
		if ($exsit_uopenid&&$sccode)//如果有这个用户，直接登录进入领积分的页面
		{
			$_SESSION['username'] = $exsit_uopenid['username'];
			$_SESSION['password'] = $exsit_uopenid['password'];
			$_SESSION['openid'] = $openid;
			//header("Location: ../user/login.php");exit;
			header("Location: ../user/jifen.php?type=hy&s=".$s."&openid=".$openid);exit;//在积分页面设置跳回首页
		}
		else 
		{
			header("Location: jiamao.php?status=3");exit;
		}
	}
}
//如果
//print_r($subscribe);
?>
<?php if (isset($sccode)&&$sccode){?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="Keywords" content="正品验证" />
<meta name="Description" content="正品验证" />
<title>香珀蒂</title>
<link rel="stylesheet" href="Public/css/home.min.css" />
</head>
<body class="ui-bar-a">
<div class="load_box" style="margin-top:-230px;">
	<div style="position:absolute;top:7px;text-align:left;left:2.5px;">你好，你所购买的产品为正品!<br/>防伪码：[<span style="color:#99CCFF"><?=$sccode?></span>]. <br/>欢迎再次购买，感谢你的支持！点击下面按钮登录领取积分换豪礼吧!更多优惠还可长按下面的二维码关注的我们的公众号了解!</div>
	<!--  
	<div class="liwuso_logo"><img src="Public/images/hbss_icon.jpg" /></div>
	<div class="lws_botton"><a href="../user/login.php&s=<//?=$sccode?>&openid=<//?=$openid?>&type=dg" >导购登录</a></div><br><br>
	<div class="lws_botton"><a href="../user/login.php&s=<//?=$sccode?>&openid=<//?=$openid?>$type=hy" >会员登录</a></div><br><br>
	<div class="lws_botton"><a href="../user/selectregister.php&s=<//?=$sccode?>&openid=<//?=$openid?>" >没注册?点这里</a></div>
	-->
	<?php
	//if($zyxx !== 1){
	?>
	<!--  <div class="lws_botton">你还没关注，请长按下面的二维码，关注后再扫码，才能领取红包</div>-->
	<?php
	//}
	//else 
	//{
	?>
	<div class="lws_botton"><a href="http://xbd.xf12315.net/main/chaikai.index.php?sk=1445313033&sh=43920&s=<?=$sccode?>&type=dg&openid=<?=$openid?>" >领取红包</a></div>
<?php //} ?>
</div>
<!-- </div> -->
<footer id="footer" class="index_footer">
	<div class="copy">
		<div style="margin-left:-90px">
			<img src="Public/images/xffw.jpg"  width="100" height="100" /><span style="float:left;position:absolute;width:100px;height:50px;">#长按关注#或者微信客户端搜索香珀蒂进行关注</span><br>
		</div>
		&copyright;2015 香珀蒂All Rights Reserved.
	</div>
</footer>
</div>
<!-- /footer -->
</body>
</html>
<?php }else{
	echo "兄弟你穿越了！回地球吧！。。。";
}?>